package com.kzd.sdk.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kzd.sdk.callback.KZD_OnItemClickListener;
import com.kzd.sdk.entity.KZD_GameMove;
import com.kzd.sdk.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;


public class KZD_GameMoveAdapter extends RecyclerView.Adapter<KZD_GameMoveAdapter.GameMoveHolder> {

    private List<KZD_GameMove> data = new ArrayList<>();

    private final Context mContext;

    private KZD_OnItemClickListener mOnItemClickListener;

    public KZD_GameMoveAdapter(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public GameMoveHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(AppUtils.getLayoutId(mContext, "kzd_item_game_move"), parent, false);
        return new KZD_GameMoveAdapter.GameMoveHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull GameMoveHolder holder, int position) {
        KZD_GameMove itemData = get(position);
        holder.tvGameMoveTitle.setText(itemData.getDescription());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setNewInstance(List<KZD_GameMove> gameMoveList) {
        if (gameMoveList == null) {
            this.data = new ArrayList<>();
        } else {
            this.data = gameMoveList;
        }
        notifyDataSetChanged();
    }

    public KZD_GameMove get(int position) {
        return data.get(position);
    }

    public void setOnItemClickListener(KZD_OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public static class GameMoveHolder extends RecyclerView.ViewHolder {

        private final TextView tvGameMoveTitle;

        public GameMoveHolder(@NonNull View itemView) {
            super(itemView);
            tvGameMoveTitle = itemView.findViewById(AppUtils.getId(itemView.getContext(), "tvGameMoveTitle"));
        }
    }
}
